function editArtikl(btn,novi){
	var et = document.getElementById("editArtiklTemplate");
	var sp = document.getElementById("edit_sifra_art");
	var h  = sp.parentNode;
	if(novi==1){
		//************ NOVI ARTIKL
		h.innerHTML = 'Novi artikl<span id="edit_sifra_art"></span>';
		et.elements["edit_art_id"].value="";
		et.elements["edit_art_name"].value="";
		et.elements["edit_art_desc"].value="";
	}else{
		//************ IZMENA ARTIKLA
		var tr = btn.parentNode.parentNode;
		var td = tr.getElementsByTagName("td");
		
		var id = trim(td[0].innerHTML);
		var name = trim(td[1].innerHTML);
		var group_id=trim(td[2].getAttribute("group"));
		var desc = trim(td[3].innerHTML);
		
		h.innerHTML = 'Izmena artikla #<span id="edit_sifra_art">'+id+'</span>';
		
		et.elements["edit_art_id"].value=id;
		et.elements["edit_art_name"].value=name;
		et.elements["edit_art_desc"].value=desc;
		
		var opts = et.elements["edit_art_group"].options;
		for(var i=0; i<opts.length; i++){
			if(opts[i].value==group_id){
				opts[i].selected = true;
			}else{
				opts[i].selected = false;
			}
		}
	}
	
	et.style.display="block";
}
function afterSaveArtikl(response){
	alert(response);
	
	var fn = document.getElementById("filter_artname");
	var fg = document.getElementById("filter_artgroup");
	var cp = document.getElementById("current");
	var action = "a=artikli";
	action += "&page="+cp.innerHTML;
	action += "&filter_artname="+fn.value;
	action += "&filter_artgroup="+fg.value;
	LoadRSDoc("artikli.php?"+action,"setContent");
}
function afterSaveArtiklGroup(response,a){
	alert(response);
	if(typeof(a)=="undefined"){
		a = "artikli_grupe";
	}
	LoadRSDoc("artikli.php?a="+a,"setContent");
}
//**************************************************
//-------------------- PAKETI ----------------------
//**************************************************
function editArtiklPaket(id){
	LoadRSDoc("artikli.php?a=artikli_paketi&mod=edit&id="+id,"ypAjaxForm");
}
function updateStavkaPrice(obj){
	var ul = obj.parentNode.parentNode;
	var lis= ul.getElementsByTagName("li");
	
	var ukupno = document.getElementById("edit_paket_price");
	var ukupno_update = document.getElementById("updateCenaPaket");
	
	var u=0;
	for(var i=0; i<lis.length; i++){
		var li = lis[i];
		var sel = li.getElementsByTagName("select")[0];
		var popust = li.getElementsByTagName("input")[0];
		
		popust.value = popust.value.replace(/[^\d\.\,\%]/g,"");
		popust.value = trim(popust.value);
		var c = parseFloat(sel.options[sel.selectedIndex].getAttribute("cena"));
		
		if(popust.value.match(/\%/)){
			popust.value = parseFloat(popust.value.replace(/\%/g,""));
			popust.value = Math.round(c*popust.value/100,2);
		}
		var p = popust.value.length>0?parseFloat(popust.value):0;
		var np = c-p;
		//var np = c-(c*p/100);
		li.setAttribute("iznos",np);
		li.title = "Iznos stavke: "+number_format(np,2);
	
		if(trim(li.getAttribute("iznos")).length){
			u += parseFloat(li.getAttribute("iznos"));
		}
	}
	if(u != parseFloat(strip_number_format(ukupno.value))){
		ukupno_update.style.display="block";
		ukupno_update.value = "<< "+number_format(u,2);
	}else{
		ukupno_update.style.display="none";
		ukupno_update.value = "";
	}
}
function artikliPaketiAddStavka(){
	var ul = document.getElementById("edit_paket_artikli");
	var li = ul.getElementsByTagName("li")[0];
	var nli = li.cloneNode(true);
	ul.appendChild(nli);
}
function artikliPaketiSave(forma){
	var els = forma.elements;
	var ul = document.getElementById("edit_paket_artikli");
	var li = ul.getElementsByTagName("li");
	
	var data = Array();
	data["paket_id"]	= trim(els["paket_id"].value);
	data["paket_name"]	= trim(els["edit_paket_name"].value);
	data["paket_start"]	= trim(els["edit_paket_start"].value);
	data["paket_end"]	= trim(els["edit_paket_end"].value);
	data["paket_cena"]	= trim(els["edit_paket_price"].value);
	data["paket_opis"]	= trim(els["edit_paket_desc"].value);
	for(var i=0; i<li.length; i++){
		data["paket_cena_"+i] = li[i].getElementsByTagName("select")[0].value;
		data["paket_popust_"+i] = li[i].getElementsByTagName("input")[0].value;
	}
	var action="a=artikli_paketi&mod=save";
	for(x in data){
		action += "&"+x+"="+escape(data[x]);
	}
	LoadRSDoc("artikli.php","afterSavePaket",action,'text',1);
}
function afterSavePaket(response){
	var tmp=response.split(/\{!\}/);
	var code=parseInt(tmp[0]);
	response=tmp[1];
	alert(response);
	if(code==1){
		closeYpAjaxForm();
		LoadRSDoc("artikli.php?a=artikli_paketi","setContent");
	}
}
function afterDeletePaket(response){
	alert(response);
	document.getElementById("AP_submit_button").click();
}
function afterChangePaketDatum(response){
	var tmp = response.split(/\{!\}/);
	var cenovnik_datum=tmp[0];
	var opcije=tmp[1];
	
	var cenDisplay = document.getElementById("cenovnikDisplay");
	cenDisplay.innerHTML = "Cenovnik: "+cenovnik_datum;
	var ul = document.getElementById("edit_paket_artikli");
	var li = ul.getElementsByTagName("li");
	for(var i=0; i<li.length; i++){
		li[i].getElementsByTagName("select")[0].innerHTML = opcije;
	}
}

function afterDeleteProfilPaket(response){
	var tmp = response.split("{!}");
	var code = parseInt(tmp[0]);
	var message = tmp[1];
	alert(message);
	if(!code){
		var forma = document.getElementById("filterProfiliPaketiForm");
		var sbmt = forma.elements["sbmt"];
		if(sbmt){
			sbmt.click();
		}
	}
}
function ProfiliPaketiNewAddItem(btn){
	ol = btn.parentNode.nextSibling.nextSibling;
	var lis = ol.getElementsByTagName("li");
	ol.appendChild(lis[lis.length-1].cloneNode(true));
}
function profiliPaketiNewMakeItems(forma){
	var ol = forma.getElementsByTagName('ol')[0];
	var input = forma.elements["items"];
	var lis = ol.getElementsByTagName("li");
	var out = ";";
	for(var i=0; i<lis.length; i++){
		var sel = lis[i].getElementsByTagName("select")[0];
		out += sel.value+";";
	}
	input.value = out;
}
function afterProfiliPaketiNew(response){
	var tmp = response.split("{!}");
	var code = parseInt(tmp[0]);
	var message = tmp[1];
	alert(message);
	if(!code){
		closeYpAjaxForm();
		LoadRSDoc("artikli.php?a=profili_paketi","setContent");
	}
}
function EditProfilePaket(btn){
	
}
function OglasKoeficijentEdit(btn,artid,artname,koef){
	var nk = prompt("Koeficijent za artikl "+artname+":",koef);
	if(nk!==null){
		if(nk.match(/^\d+[\.\d+]*$/)){
			nk = parseFloat(nk>0?nk:0);
		}else if(nk.match(/^\d+[\/*\+\-\%]\d+$/)){
			nk = eval(nk);
		}else{
			nk=0;
		}
		LoadRSDoc('artikli.php?a=oglas_koeficijent&mod=save&artid='+artid+"&k="+nk,'afterOglasKoeficijentSave');
	}
}
function afterOglasKoeficijentSave(response){
	var tmp = response.split("{!}");
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	alert(msg);
	if(!code){
		LoadRSDoc('artikli.php?a=oglas_koeficijent','setContent');
	}
}